#!/usr/bin/python3
import sys
import re

def str_hash(string):
    value = 0
    for c in string:
        value += ord(c)
        value *= 17
        value %= 256
    return value

def part1():
    print('part 1')
    [print(string, '\t', str_hash(string)) for string in sys.stdin.readline().split(',')]
    total = sum([str_hash(string) for string in sys.stdin.readline().split(',')])
    print(total)

def lens_in_box(box, label, num):
    for i in range(len(box)):
        if box[i][0] == label:
            box[i][1] = num
            return True
    return False

def print_boxes(boxes):
    for box in boxes:
        if len(box) > 0:
            print(box)

def sum_focusing_power(boxes):
    total = 0
    for i, box in enumerate(boxes):
        if len(box) > 0:
            for j, (label, num) in enumerate(box):
                total += (i + 1) * (j + 1) * int(num)
    return total

def part2():
    print('part 2')
    instructions = sys.stdin.readline().split(',')
    boxes = [list() for _ in range(256)]
    print(boxes)
    for inst in instructions:
        label, sign, num = re.findall(r'([a-z]+)(=|-)([0-9])?', inst)[0]
        print(f'{inst} ({label} {sign} {num})\t{str_hash(label)}')
        box = boxes[str_hash(label)]
        if num:
            if not lens_in_box(box, label, num):
                box.append([label, num])
        else:
            for i in range(len(box)):
                if box[i][0] == label:
                    del box[i]
                    break

        print_boxes(boxes)
    
    print(sum_focusing_power(boxes))

if sys.argv[1] in '1':
    part1()
else:
    part2()
